
#pragma once

#ifndef VBO_EXT
#include <VBO_Ext.h>
#define VBO_EXT

#endif

#include <gl\glaux.h>	

#define MESH_RESOLUTION 4.0f									// Pixels Per Vertex
#define MESH_HEIGHTSCALE 1.0f									// Mesh Height Scale


// VBO Extension Definitions, From glext.h
#define GL_ARRAY_BUFFER_ARB 0x8892
#define GL_STATIC_DRAW_ARB 0x88E4


class CVert														// Vertex Class
{
public:
	float x;													// X Component
	float y;													// Y Component
	float z;													// Z Component
};
typedef CVert CVec;	

class CTexCoord													// Texture Coordinate Class
{
public:
	float u;													// U Component
	float v;													// V Component
};


class CMesh
{

public:
	bool g_fVBOSupported;
	int m_nVertexCount;
	CVert* m_pVertices;	
	CTexCoord* m_pTexCoords;
	unsigned int m_nTextureId;

	unsigned int m_nVBOVertices;
	unsigned int m_nVBOTexCoords;

	AUX_RGBImageRec* m_pTextureImage;

public :
	CMesh();
	~CMesh();

	bool LoadHeightMap(char *szPath,float flHeightScale,float flResolution);

	float PtHeight(int nX, int nY);

	void BuildVBOs();
};

class VBO
{
public :
	static bool IsExtensionSupported(char* szTargetExtension)
	{
		const unsigned char* pszExtensions = NULL;
		const unsigned char* pszStart;
		unsigned char* pszWhere, *pszTerminator;

		pszWhere = (unsigned char *) strchr(szTargetExtension, ' ');

		if(pszWhere || *szTargetExtension == '\0')
			return false;

		pszExtensions = glGetString(GL_EXTENSIONS);

		pszStart = pszExtensions;

		for(;;)
		{
			pszWhere = (unsigned char *)strstr((const char * )pszStart,szTargetExtension);
			if(!pszWhere)break;

			pszTerminator = pszWhere + strlen(szTargetExtension);
			if(pszWhere == pszStart || *(pszWhere -1) == ' ')
				if(*pszTerminator == ' ' || *pszTerminator == '\0')
					return true;

			pszStart = pszTerminator;
		}

		return false;
	}
};

